#ifndef SEQLIST_H
#define SEQLIST_H

#include "List.h"
#include "Exception.h"

namespace StructLib
{
    template <typename T>
    class SeqList : public List<T>
    {
    protected:
        T* m_array;
        int m_length;
    public:
        bool Insert(const T& e)
        {
            return Insert(m_length, e);
        }

        bool Insert(int i, const T& e)
        {
            bool ret = ( (i <= m_length) && (i >= 0) );
            ret = ret && (m_length < Capacity());

            if (ret)
            {
                for (int p = m_length - 1;  p >= i; p--)
                {
                    m_array[p + 1] = m_array[p];
                }
                m_array[i] = e;
                m_length++;
            }

            return ret;
        }

        bool Remove(int i)
        {
            bool ret = ( (i < m_length) && (i >= 0) );
            if (ret)
            {
                for (int p = i; i < m_length - 1; p++)
                {
                    m_array[p] = m_array[p + 1];
                }
                m_length--;
            }

            return ret;
        }

        bool Set(int i, const T& e)
        {
            bool ret = ( (i < m_length) && (i >= 0) );
            if (ret)
            {
                m_array[i] = e;
            }

            return ret;
        }

        bool Get(int i, T& e) const
        {
            bool ret = ( (i < m_length) && (i >= 0) );
            if (ret)
            {
                e = m_array[i];
            }

            return ret;
        }

        int Length() const
        {
            return m_length;
        }

        void Clear()
        {
            m_length = 0;
        }

        T& operator [] (int i)
        {
            if ( (i < m_length) && (i >= 0) )
            {
                return m_array[i];
            }
            else
            {
                THROW_EXCEPTION(IndexOutOfBoundsException, "Parameter i is invalid...");
            }
        }

        T operator [] (int i) const
        {
            return (const_cast<SeqList<T>&>(*this))[i];
        }

        virtual int Capacity() const = 0;
    };
}


#endif // !SEQLIST_H
